# -*- coding: utf-8 -*-
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from django.db.models import Q
from mdcsapp.models import PlayerUserProfile, Match
from mdcsapp.statistics import Statistics

def statistics(request):
    return render_to_response('stats/statistics.html', {}, context_instance=RequestContext(request))

def chess_statistics(request):
    home_users = Q(homes__game_type=1, homes__winner__isnull=False)
    away_users = Q(aways__game_type=1, aways__winner__isnull=False)
    users = PlayerUserProfile.objects.filter(home_users | away_users).distinct().order_by('-chess_points', 'id')
    matches = Match.objects.filter(game_type=1).filter(winner__isnull=False).order_by('-date_played').order_by('-id') \
        .prefetch_related('home__user', 'away__user')
    chess_stat = Statistics(users, matches, 1)

    return render_to_response('stats/chess_statistics.html', {"chess_stat": chess_stat},
        context_instance=RequestContext(request))

def tf_statistics(request):
    home_users = Q(homes__game_type=2, homes__winner__isnull=False)
    away_users = Q(aways__game_type=2, aways__winner__isnull=False)
    users = PlayerUserProfile.objects.filter(home_users | away_users).distinct().order_by('-tf_points', 'id')
    matches = Match.objects.filter(game_type=2).filter(winner__isnull=False).order_by('-date_played').order_by('-id') \
        .prefetch_related('home__user', 'away__user')
    tf_stat = Statistics(users, matches, 2)

    return render_to_response('stats/tf_statistics.html', {"tf_stat": tf_stat},
        context_instance=RequestContext(request))

def football_statistics(request):
    home_users = Q(homes__game_type=3, homes__winner__isnull=False)
    away_users = Q(aways__game_type=3, aways__winner__isnull=False)
    users = PlayerUserProfile.objects.filter(home_users | away_users).distinct().order_by('-football_points', 'id')
    matches = Match.objects.filter(game_type=3).filter(winner__isnull=False).order_by('-date_played').order_by('-id') \
        .prefetch_related('home__user', 'away__user')
    football_stat = Statistics(users, matches, 3)

    return render_to_response('stats/football_statistics.html', {"football_stat": football_stat},
        context_instance=RequestContext(request))

def basket_statistics(request):
    home_users = Q(homes__game_type=4, homes__winner__isnull=False)
    away_users = Q(aways__game_type=4, aways__winner__isnull=False)
    users = PlayerUserProfile.objects.filter(home_users | away_users).distinct().order_by('-basket_points', 'id')
    matches = Match.objects.filter(game_type=4).filter(winner__isnull=False).order_by('-date_played').order_by('-id') \
        .prefetch_related('home__user', 'away__user')
    basket_stat = Statistics(users, matches, 4)

    return render_to_response('stats/basket_statistics.html', {"basket_stat": basket_stat},
        context_instance=RequestContext(request))